﻿/// <reference path="Common.js" />
/// <reference path="Until.js" />
/// <reference path="jquery-1.11.0.js" />
/// <reference path="MicrosoftAjax.js" />

///Other: quannm

Type.registerNamespace('Lib');

Lib.textBox = function (element) {
    Lib.textBox.initializeBase(this, [element]);
    this._typeData = null;
    this._onClientChange = null;
    this._upperCase = null;
    this._lke = null;
    this._onClientBlur = null;
    this._onClientClick = null;
    this._onClinetDblClick = null;
    this._urlReference = null;
    this._checkDate = null;
    this._Msg = null;
    this._Name = null;
    this._IsInputData = null;
    this._SaveTypeData = null;
    this.__Decimal = null;
    this._IsReset = null;


}
//Thuộc tính
Lib.textBox.prototype = {
    initialize: function () {
        Lib.textBox.callBaseMethod(this, 'initialize');
        //Thêm event
        $addHandlers(this.get_element(),
                          {
                              // 'keydown': this._onKeyDown,
                              'focus': this._onFocus,
                              'blur': this._onBlur,
                              // 'keypress': this._onKeyPress,
                              'click': this._onClick,
                              'dblclick': this._onDblClick
                          },
                          this);

        var control = this.get_element();
        var typeData = this.get_typeData();
        var lke = this.get_lke();
        var upperCase = this.get_upperCase();
        var nameControl = this.get_Name();
        var msg = this.get_Msg();
        var checkDate = this.get_CheckDate();

        $(this.get_element()).attr("onchange", this.get_clientChange());
        $(this.get_element()).keypress(function (e) {
            var keyCode = Common.getKeyCode(e, "keypress");
            if (keyCode <= 0 || keyCode == Sys.UI.Key.enter) return;
            $(this).attr("isChange", "true");
            var position = Common.getCaret(control);
            if (typeData.toString() == "String") {
                if (upperCase == true) {
                    var newVal = control.value.toString().substring(0, position) +
                    Common.formCodeToChar(keyCode).toUpperCase() + control.value.toString().substring(position);
                    $(control).val(newVal);
                    Common.setlectRang(control, position + 1, position + 1);
                }
                else {
                    control.value = control.value.toString().substring(0, position) +
                   Common.formCodeToChar(keyCode) + control.value.toString().substring(position);
                    Common.setlectRang(control, position + 1, position + 1);
                }
                if (lke != "" && lke != null) {
                    var bVal = control.value;
                    var newVal = Common.formCodeToChar(keyCode).toUpperCase();;
                    if (!IsInString(this.lke, newVal)) {
                        if (newVal != bVal)
                            $(control).val(newVal);

                        e.preventDefault();
                    }
                }
                e.preventDefault();
                return false;
            }
            if (typeData.toString() == "Number") {
                if (!Common.isKeyDigit(keyCode)) { e.preventDefault(); return true; }
            }
            else {
                if (typeData.toString() == "Date" && position >= 10) { e.preventDefault(); return true; }
                if (typeData.toString() == "Month" && position >= 7) { e.preventDefault(); return true; }

                if (position == 2 || (position == 5 && this._typeData.toString() == "Date"))
                    position++;

                var newVal = control.value.toString().substring(0, position) +
                     Common.formCodeToChar(keyCode) + control.value.toString().substring(position + 1);
                Common.setlectRang(control, position + 1, position + 1);
                $(control).val(newVal);

                e.preventDefault();
                return false;
            }
        });

        $(this.get_element()).keydown(function (e) {
            var keyCode = Common.getKeyCode(e, "keydown");
            switch (keyCode) {
                case Sys.UI.Key.tab:
                    if (e.shiftKey) {
                        Common.onPreviousControl(control, e);
                    }
                    else
                        Common.onNextControl(control, e);
                    break;
                case Sys.UI.Key.tab:
                case Sys.UI.Key.enter:
                    if ($(this).attr("isChange") == "true") {
                        $(this).trigger("change");
                        $(this).attr("isChange", "false");
                    }
                    if (nameControl == nameControl.toUpperCase()) {
                        if ($(control).val() == "") {
                            Msg.Show("Cảnh báo", "Không được nhập trống " + (msg == null ? "" : msg), "error");
                            $(control).focus();
                            return false;
                        }
                    }
                    if (checkDate) {
                        var b_value = $(control).val();
                        var a_value = b_value.split('/');
                        var b_thang = 0, b_nam = 0;
                        var ok = false;
                        if (this._typeData.toString() == "Date") {
                            b_thang = parseInt(a_value[1].toString());
                            b_nam = a_value[2].toString();
                            ok = true;
                        }
                        else {
                            b_thang = parseInt(a_value[0].toString());
                            b_nam = a_value[1].toString();
                            ok = false;
                        }

                        if (b_thang < 0 || b_thang > 12) {
                            Msg.Show("Cảnh báo", "Tháng không hợp lệ", "error");
                            $(this._element).focus();
                        }

                        if (b_nam.length <= 3) {
                            Msg.Show("Cảnh báo", "Năm không hợp lệ", "error");
                            $(this._element).focus();
                        }
                        if (ok) {
                            var b_ngay = Common.DaysOfMonth(b_thang, b_nam);
                            var day = parseInt(a_value[0]);
                            if (day < 0 || day > b_ngay) {
                                Msg.Show("Cảnh báo", "Ngày không hợp lệ", "error");
                                $(control).focus();
                            }
                        }

                    }
                    Common.onNextControl(control, e);
                    break
            }
        });

    },
    dispose: function () {
        //Xóa mọi event
        $clearHandlers(this.get_element());
        Lib.textBox.callBaseMethod(this, 'dispose');
    },
    _onClick: function (e) {
        var control = this.get_element();
        var keycode = Common.getKeyCode(e);
        var bVal = control.value;
        if (!Common.isNullOrEmty(this._onClientClick))
            window.setTimeout(this._onClientClick, 0);

        if (this._lke != "" && this._lke != null) {
            $(control).val(Common.transformValue(bVal, this._lke));
            Common.setlectRang(control, 0, 0);
        }
        e.preventDefault();
    },
    _onKeyDown: function (e) {
        var control = this.get_element();
        var keycode = Common.getKeyCode(e);
        var position = Common.getCaret(control);

        if (keycode == Sys.UI.Key.backspace) {
            if (position <= 0) { return true; }
            if ("Number,String".indexOf(this._typeData.toString()) >= 0) return true;
            if (position == 3 || (position == 6 && this._typeData.toString() == "Date"))
                position = position - 1;
            var val = $(control).val().substring(0, position - 1) + "_" + $(control).val().substring(position);
            $(control).val(val);
            Common.setlectRang(control, position - 1, position - 1);
            e.preventDefault();
        }
        else if (keycode == Sys.UI.Key.del) {
            if ("Number,String".indexOf(this._typeData.toString()) >= 0) return true;
            var check = false;
            if (this._typeData.toString() == "Date" && position >= 10) { e.preventDefault(); return true; }
            if (this._typeData.toString() == "Month" && position >= 7) { e.preventDefault(); return true; }

            if (position == 2 || (position == 5 && check)) {
                var newVal = $(control).val().substring(0, position + 1) +
                        $(control).val().substring(position + 2, $(control).val().length) + "_";
                $(control).val(newVal);
            }
            else {
                var newVal = $(control).val().substring(0, position) +
                     $(control).val().substring(position + 1, $(control).val().length) + "_";
                $(control).val(newVal);
            }
            var s = $(control).val().replace(/\//g, "");
            var valInput = s.substring(0, 2) + "/" + s.substring(2, 4) + "/" + s.substring(4);
            $(control).val(valInput);
            Common.setlectRang(control, position, position);
            e.preventDefault();
        }
        else if (keycode == Sys.UI.Key.tab) {
            if (this.get_CheckDate()) {
                var b_value = this.get_value();
                var a_value = b_value.split('/');
                var b_thang = 0, b_nam = 0;
                var ok = false;
                if (this._typeData.toString() == "Date") {
                    b_thang = parseInt(a_value[1].toString());
                    b_nam = a_value[2].toString();
                    ok = true;
                }
                else {
                    b_thang = parseInt(a_value[0].toString());
                    b_nam = a_value[1].toString();
                    ok = false;
                }

                if (b_thang < 0 || b_thang > 12)
                    return alert("Tháng không hợp lệ");


                if (b_nam.length <= 3) return alert("Năm không hợp lệ");
                if (ok) {
                    var b_ngay = Common.DaysOfMonth(b_thang, b_nam);
                    var day = parseInt(a_value[0]);
                    if (day < 0 || day > b_ngay)
                        return alert("Ngày không hợp lệ");
                }
            }
            Common.onNextControl(control, e);
        }
        else if (keycode == Sys.UI.Key.left) {
            Common.onNextControl(control, e);
        }
        else if (keycode == Sys.UI.Key.end || keycode == Sys.UI.Key.home ||
           keycode == Sys.UI.Key.right) {
            return true;
        }
            //F1
        else if (keycode == 112) {
            var b_url = this.get_UrlReference();
            if (Common.isNullOrEmty(b_url)) return alert("");
        }
    },

    _onFocus: function (e) {
        var control = this.get_element();
        if ("Number,String".indexOf(this._typeData.toString()) >= 0) return true;
        if (this._typeData.toString() == "Month" && !Common.containsDigits(control.value))
            control.value = "__/____"
        else if (this._typeData.toString() == "Date" && !Common.containsDigits(control.value))
            control.value = "__/__/____";
        Common.setlectRang(control, 0, 0);
    },

    _onBlur: function (b_control) {
        var control = this.get_element();
        if (!Common.isNullOrEmty(this._onClientBlur))
            window.setTimeout(this._onClientBlur, 0);
        if ("Number,String".indexOf(this._typeData.toString()) >= 0) return true;
        if (this._typeData.toString() == "Date" || this._typeData.toString() == "Month")
            if (!Common.containsDigits(control.value))
                control.value = "";
    },
    _onDblClick: function (e) {
        if (!Common.isNullOrEmty(this._onClinetDblClick))
            window.setTimeout(this._onClinetDblClick, 0);
        e.preventDefault();
    },
    get_value: function () {
        var control = this.get_element();
        return control.value;
    },
    set_value: function (value) {
        $(this.get_element()).val(value);
    },
    get_typeData: function () {
        return this._typeData;
    },

    set_typeData: function (value) {
        if (this._typeData !== value) {
            this._typeData = value;
        }
    },
    get_upperCase: function () {
        return this._upperCase;
    },

    set_upperCase: function (value) {
        if (this._upperCase !== value) {
            this._upperCase = value;
        }
    },
    get_lke: function () {
        return this._lke;
    },

    set_lke: function (value) {
        if (this._lke !== value) {
            this._lke = value;
        }
    },
    get_clientChange: function () {
        return this._onClientChange;
    },
    set_clientChange: function (value) {
        if (this._onClientChange != value) {
            this._onClientChange = value;
        }
    },
    get_onClientBlur: function () {
        return this._onClientBlur;
    },

    set_onClientBlur: function (value) {
        if (this._onClientBlur !== value) {
            this._onClientBlur = value;
        }
    },
    get_onClientClick: function () {
        return this._onClientClick;
    },

    set_onClientClick: function (value) {
        if (this._onClientClick !== value) {
            this._onClientClick = value;
        }
    },
    get_onClinetDblClick: function () {
        return this._onClinetDblClick;
    },

    set_onClinetDblClick: function (value) {
        if (this._onClinetDblClick !== value) {
            this._onClinetDblClick = value;
        }
    },
    get_UrlReference: function () {
        return this._urlReference;
    },

    set_UrlReference: function (value) {
        if (this._urlReference !== value) {
            this._urlReference = value;
        }
    },
    get_CheckDate: function () {
        return this._checkDate;
    },

    set_CheckDate: function (value) {
        if (this._checkDate !== value) {
            this._checkDate = value;
        }
    },
    get_Msg: function () {
        return this._Msg;
    },
    set_Msg: function (value) {
        if (this._Msg !== value) {
            this._Msg = value;
        }
    },
    get_Name: function () {
        return this._Name;
    }
    ,
    set_Name: function (value) {
        if (this._Name !== value)
            this._Name = value;
    },
    get_IsInputData: function () {
        return this._IsInputData;
    }
    ,
    set_IsInputData: function (value) {
        if (this._IsInputData !== value)
            this._IsInputData = value;
    },
    get_SaveTypeData: function () {
        return this._SaveTypeData;
    }
    ,
    set_SaveTypeData: function (value) {
        if (this._SaveTypeData !== value)
            this._SaveTypeData = value;
    },
    get_Decimal: function () {
        return this._Decimal;
    }
    ,
    set_Decimal: function (value) {
        if (this._Decimal !== value)
            this._Decimal = value;
    },
    get_IsReset: function () {
        return this._IsReset;
    }
    ,
    set_IsReset: function (value) {
        if (this._IsReset !== value)
            this._IsReset = value;
    }
}
Lib.textBox.descriptor = {
    properties: [{ name: 'typeData', type: String },
                 { name: 'clientChange', type: String },
                 { name: 'upperCase', type: String },
                 { name: 'lke', type: String },
                 { name: 'onClientBlur', type: String },
                 { name: 'onClientClick', type: String },
                 { name: 'onClinetDblClick', type: String },
                 { name: 'UrlReference', type: String },
                 { name: 'CheckDate', type: String },
                 { name: 'Msg', type: String },
                 { name: 'Name', type: String },
                 { name: 'IsInputData', type: String },
                 { name: 'SaveTypeData', type: String },
                 { name: 'Decimal', type: String },
                 { name: 'IsReset', type: String }
    ]
}

Lib.textBox.registerClass('Lib.textBox', Sys.UI.Control);


if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();